package com.deer.wms.instock.service.enter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.base.system.model.PurchaseOrderBO;
import com.deer.wms.instock.model.erpType.ErpType;
import com.deer.wms.instock.service.enter.EnterErp;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import org.jsoup.helper.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: xu
 * @Date: 2022/06/20/11:20
 * @Description: 251300056返修拆改通知单，10其他入库单
 */@Component

public class Repair implements EnterErp {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;


    @Override
    public ErpType gainType() {
      return   new ErpType(251300056, 10);
    }

    @Override
    public Map<String, String> EnterBeanAndPush(AsnMaster asnMaster, List<AsnDetail> asnDetails, BaseAssist enterAssist) {
        OrderToErpBO orderToErpBO = new OrderToErpBO();
        List<OrderDetailBO> details = new ArrayList<>();
        orderToErpBO.setBillTypeId(asnMaster.getAsnType());
        orderToErpBO.setSourceBillTypeId(asnMaster.getBillType());
        orderToErpBO.setBillDate(DateUtils.getNowDateString()); //日期
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("INPUT"));
        if (ObjectUtil.isNotEmpty(asnMaster.getDeptId())) {
        Map<String, Object> map = new HashMap<>();
            map.put("billTypeId", asnMaster.getBillType());
            map.put("billIds", Arrays.asList(asnMaster.getBeforeNum()));
            List<PurchaseOrderBO> purchaseOrderBOS = pushErpService.GetAllIMInSourceBills(map);
            asnMaster.setDeptId(purchaseOrderBOS.listIterator().next().getDeptId());
        }
        orderToErpBO.setDeptId(asnMaster.getDeptId());
        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(enterAssist.getCategoryId()) ? enterAssist.getCategoryId() : 27972); //默认其他调拨
        for (AsnDetail asnDetail : asnDetails) {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setSourceBillId(asnDetail.getSourceId()); //源单id
            orderDetailBO.setSourceBillDetailRowIndex(asnDetail.getRowIndex()); //行号
            orderDetailBO.setBatchNo(asnDetail.getBatchName()); //批号
            orderDetailBO.setQty(asnDetail.getQty()); //数量
//            orderDetailBO.setAuxPropId(asnDetail.getUpperPackDetailId()); //辅助类型id  33469
            orderDetailBO.setStockId(asnDetail.getStockId()); //
            orderDetailBO.setProductionDate(DateUtils.getNowDateString()); //日期
            details.add(orderDetailBO);
        }
        orderToErpBO.setDetails(details);
        return pushErpService.pushIMBIll(orderToErpBO);

    }
}
